package net.zjitc.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import net.zjitc.pojo.AdminUser;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.AdminService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/23 15:05
 * @description:
 */
@Controller
public class AdminLogin {
  @Autowired
  private AdminService adminService;
  @Autowired
  private CategoryService categoryService;
  @Autowired
  private ProductService productService;

  //管理员登录
  @RequestMapping("/adminuserLogin")
  public ModelAndView Adminlogin(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      HttpSession session)
  {
    AdminUser user = adminService.AdminLogin(username,password);
    ModelAndView mv = new ModelAndView();
    if (user!=null){
      session.setAttribute("admin",user);
      mv.setView(new RedirectView("shelll68/content.jsp"));
      return mv;
    }
    mv.setView(new RedirectView("shelll68/index.jsp"));
    return mv;
  }


  //管理员退出
  @RequestMapping("/loginOut")
  public ModelAndView loginOut(HttpSession session){
    session.removeAttribute("admin");
    ModelAndView mv = new ModelAndView();
    mv.setView(new RedirectView("shelll68/index.jsp"));
    return mv;
  }
  //跳转到首页
  @RequestMapping("/index")
  public ModelAndView index(){
    ModelAndView mv =new ModelAndView();
    List<Category> clist=categoryService.findAll();
    mv.addObject("clist",clist);
    List<Product> phlist=productService.findHot();
    mv.addObject("phlist",phlist);
    mv.setView(new InternalResourceView("user/index.jsp"));
    List<Product> pnewlist=productService.findNew();
    mv.addObject("pnewlist",pnewlist);
    return mv;
  }
  //查询用户
  @RequestMapping("/findAllUser")
  public ModelAndView findAllUser(){
    ModelAndView mv = new ModelAndView();
    List<User> user = adminService.findAllUser();
    mv.addObject("user",user);
    mv.setView(new InternalResourceView("shelll68/column.jsp"));
    return mv;
  }
  //删除用户
  @RequestMapping("/deleteUser")
  public ModelAndView deleteUser(
      @RequestParam(name = "uid") Integer uid
  ){
    ModelAndView mv =new ModelAndView();
    System.out.println(uid);
    Integer count = adminService.DeleteUser(uid);
    mv.setView(new InternalResourceView("/findAllUser.do"));
    return mv;
  }
  //跳转登陆页面
  @RequestMapping("/loginUI"  )
  public ModelAndView loginUI(){
    ModelAndView modelAndView=new ModelAndView();
    List<Category> clist=categoryService.findAll();
    modelAndView.addObject("clist",clist);
    modelAndView.setView(new InternalResourceView("user/login.jsp"));
    return modelAndView;
  }
  //跳转到注册页面
  @RequestMapping("/registUI")
  public ModelAndView registUI(){
    ModelAndView modelAndView =new ModelAndView();
    List<Category> clist=categoryService.findAll();
    modelAndView.addObject("clist",clist);
    modelAndView.setView(new InternalResourceView("user/regist.jsp"));
    return modelAndView;
  }


//用户登陆
  @RequestMapping("/user_login")
  public ModelAndView user_login(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      @RequestParam(name = "inputvail")String inputvail,
      HttpSession session){
    ModelAndView mv = new ModelAndView();
    String truevail= (String) session.getAttribute("vaildateCode");
    //equalsIgnoreCase:忽视大小写
    if (!inputvail.equalsIgnoreCase(truevail)){
      mv.addObject("codeMsg","验证码有误");
      //菜单栏显示一级菜单
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("user/login.jsp"));
      return mv;
    }
    User user=adminService.findByCondition(username,password);
    if (user==null || (user.getState()==0)){
      //菜单栏显示一级菜单
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.addObject("lmsg","用户名或密码错误,或者还未激活");
      mv.setView(new InternalResourceView("user/login.jsp"));
    }else {
      //登录成功
      session.setAttribute("session_user",user);
      mv.setView(new InternalResourceView(("/index.do")));
    }
    return mv;
  }
//用户注册
  @RequestMapping("/regist")
  public ModelAndView regist(
      User user,
      @RequestParam(name = "inputvail")String inputvail,
      HttpSession session){
    ModelAndView mv =new ModelAndView();
    String truevail= (String) session.getAttribute("vaildateCode");
    //equalsIgnoreCase:忽视大小写
    if (!inputvail.equalsIgnoreCase(truevail)){
      mv.addObject("codeMsg","验证码有误");
      //菜单栏显示一级菜单
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("user/regist.jsp"));
      return mv;
    }
    //用户名错误
    String UserName= (String) session.getAttribute("username");
    boolean flagusername=user.getUsername().matches("^[a-zA-Z]\\w{2,11}$");
    if (flagusername!=true){
      mv.addObject("msg","用户名格式错误");
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("user/regist.jsp"));
      return mv;
    }
    //密码格式错误
    String PassWord= (String) session.getAttribute("password");
    boolean flagpassword=user.getPassword().matches("^[a-zA-Z0-9]{6,12}$");
    if (flagpassword!=true){
      mv.addObject("passwordMsg","密码格式错误");
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("user/regist.jsp"));
      return mv;
    }
    //电话格式错误
    String Phone= (String) session.getAttribute("phone");
    boolean flagphone=user.getPhone().matches("^1[3578]\\d{9}$");
    if (flagphone!=true){
      mv.addObject("phoneMsg","电话格式错误");
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("user/regist.jsp"));
      return mv;
    }
    //邮箱格式错误
    String Email= (String) session.getAttribute("email");
    boolean flagemail=user.getEmail().matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
    if (flagemail!=true){
      mv.addObject("emailMsg","邮箱格式错误");
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("user/regist.jsp"));
      return mv;
    }

    //接收用户信息
    try {
      Integer count=adminService.insert(user);
    } catch (Exception e) {
      e.printStackTrace();
    }
    List<Category> clist=categoryService.findAll();
    mv.addObject("clist",clist);
    mv.setView(new InternalResourceView("/user/regismsg.jsp"));
    return mv;
  }
//用户激活
  @RequestMapping("/activeCode")
  public ModelAndView activeCode(
      @RequestParam(name = "code") String code
  ){
        ModelAndView mv =new ModelAndView();
    //通过code更改激活状态
    Integer integer =adminService.findByCode(code);
    mv.addObject("acmsg","激活成功，请登录");
    mv.setView(new InternalResourceView(("/index.do")));
    return mv;
  }
//退出登录
  @RequestMapping("/user_exit")
  public ModelAndView user_exit(HttpSession session){
    ModelAndView mv =new ModelAndView();
    session.removeAttribute("session_user");

    mv.setView(new RedirectView(("/index.do")));
    return mv;
  }
}